//
//  EFRequestReturnViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/23.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFRequestReturnViewController.h"
#import "EFChooseReturnGoodsViewController.h"
#import "EFReturnListViewController.h"
#import "EFReturnGoodsViewController.h"
#import "EFRetrunReasonItem.h"
#import "EFAddressMarkViewPicker.h"
#import "EFH5ViewController.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "iToast.h"

@interface EFRequestReturnViewController ()


@property(strong,nonatomic)UILabel* receiverLabel;
@property(strong,nonatomic) UITextField *receiverTextF;
@property(strong,nonatomic)UIView* receiverLineView;


@property(strong,nonatomic)UILabel* phoneLabel;
@property(strong,nonatomic) UITextField *phoneTextF;
@property(strong,nonatomic)UIView* phoneLineView;


@property(strong,nonatomic)UILabel* markTipsLabel;
@property(strong,nonatomic)UILabel* markLabel;
@property(strong,nonatomic)UIImageView* markMoreIV;
@property(strong,nonatomic)UIView* markLineView;


@property(strong,nonatomic)UILabel* desLabel;
@property(strong,nonatomic) UITextField *desTextF;
@property(strong,nonatomic)UIView* desLineView;


@property(strong,nonatomic) UIButton *agreeBtn;
@property(strong,nonatomic) UILabel *introductionLabel;


@property(strong,nonatomic) UIButton *nextBtn;
@property(strong,nonatomic) NSString *orderNo;
@property(strong,nonatomic) NSMutableArray *reasonArray;
@property(strong,nonatomic) EFRetrunReasonItem *reason;

@property(strong,nonatomic)EFAddressMarkViewPicker* addressPicker;

@property (strong, nonatomic) TPKeyboardAvoidingScrollView * scrollView;

@end

@implementation EFRequestReturnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"申请退货";
        self.reasonArray = [NSMutableArray array];
    }
    return self;
}

-(instancetype)initWithOrderNo:(NSString *)orderNo{
    self = [self init];
    if (self) {
        self.orderNo = orderNo;
    }
    return self;
}


-(void)initView{
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    [self.scrollView addSubview:self.receiverLabel];
    [self.scrollView addSubview:self.receiverTextF];
    [self.scrollView addSubview:self.receiverLineView];
    [self.scrollView addSubview:self.phoneLabel];
    [self.scrollView addSubview:self.phoneTextF];
    [self.scrollView addSubview:self.phoneLineView];
    [self.scrollView addSubview:self.markTipsLabel];
    [self.scrollView addSubview:self.markLabel];
    [self.scrollView addSubview:self.markMoreIV];
    [self.scrollView addSubview:self.markLineView];
    [self.scrollView addSubview:self.desLabel];
    [self.scrollView addSubview:self.desTextF];
    [self.scrollView addSubview:self.desLineView];
    
    [self.scrollView addSubview:self.agreeBtn];
    [self.scrollView addSubview:self.introductionLabel];
    [self.scrollView addSubview:self.nextBtn];
    [self.view addSubview:self.scrollView];
}

-(void)initData{
    [APP_CONTEXT.net getReturnReason:@"1" OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                for (NSDictionary *dic in array) {
                    EFRetrunReasonItem *item = [EFRetrunReasonItem yy_modelWithDictionary:dic];
                    [self.reasonArray addObject:item];
                }
            }
        }
    }];
}

-(void)showToastMessage:(NSString*) message{
    [[iToast makeText:message] show];
}

-(void)onClickAdd{
    
    NSString* name =  self.receiverTextF.text;
    if(name == nil || name.length <=0){
        [self showToastMessage:@"联系人不能为空"];
        return;
    }
    
    NSString* phone =  self.phoneTextF.text;
    if(phone == nil || phone.length <=0){
        [self showToastMessage:@"联系方式不能为空"];
        return;
    }
    
    if(self.reason == nil  ){
        [self showToastMessage:@"退货原因不能为空"];
        return;
    }
    
    NSString* reason =  self.markLabel.text;
    NSString* desc =  self.desTextF.text;
    if([reason isEqualToString:@"其他"]  && (desc ==nil || desc.length <=0  )){
        [self showToastMessage:@"需填写具体原因"];
        return;
    }
    
    if(!self.agreeBtn.selected){
        [self showToastMessage:@"未同意说明"];
        return;
    }
    
    EFReturnGoodsViewController* returnGood = [[EFReturnGoodsViewController alloc]initWithOrderNo:self.orderNo receiver:name phone:phone reason:self.reason desc:desc];
    returnGood.orderId = self.orderId;
    [ICNavigator pushViewController:returnGood animated:YES];
}

-(void)onClickStaffBtn:(id) sender{
    UIButton* btn = (UIButton*)sender;
    btn.selected = !btn.selected;
}


-(TPKeyboardAvoidingScrollView*)scrollView{
    if(_scrollView == NULL){
        _scrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame: CGRectMake(0, 0, kScreenWidth, kScreenHeight -kStatusBarHeight)];
        _scrollView.contentSize =  CGSizeMake(kScreenWidth, kScreenHeight - kStatusBarHeight);
    }
    return _scrollView;
}



-(UILabel *)receiverLabel{
    if(_receiverLabel == NULL){
        _receiverLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _receiverLabel.left = 20;
        _receiverLabel.top = 19;
        _receiverLabel.font = [UIFont systemFontOfSize:15];
        _receiverLabel.textColor = kBlack333;
        NSMutableAttributedString * strDes = [[NSMutableAttributedString alloc]initWithString:@"联系人*"];
        NSRange range = NSMakeRange(3,1);
        [strDes addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [_receiverLabel setAttributedText:strDes];
        [_receiverLabel sizeToFit];
    }
    return _receiverLabel;
}

-(UITextField *)receiverTextF{
    if(_receiverTextF == NULL){
        _receiverTextF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 100, 17)];
        _receiverTextF.left = self.receiverLabel.right + 35;
        _receiverTextF.top = 19;
        _receiverTextF.font = [UIFont systemFontOfSize:15];
        _receiverTextF.textColor = kBlack999;
        _receiverTextF.keyboardType = UIKeyboardTypeDefault;
        _receiverTextF.placeholder = @"填写联系人";
    }
    
    return _receiverTextF;
}


-(UIView *)receiverLineView{
    if(_receiverLineView == NULL){
        _receiverLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _receiverLineView.left = 20;
        _receiverLineView.top = self.receiverLabel.bottom + 11;
        _receiverLineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _receiverLineView;
}


-(UILabel *)phoneLabel{
    if(_phoneLabel == NULL){
        _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _phoneLabel.left = 20;
        _phoneLabel.top = self.receiverLineView.bottom + 19;
        _phoneLabel.font = [UIFont systemFontOfSize:15];
        _phoneLabel.textColor = kBlack333;
        NSMutableAttributedString * strDes = [[NSMutableAttributedString alloc]initWithString:@"联系方式*"];
        NSRange range = NSMakeRange(4,1);
        [strDes addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [_phoneLabel setAttributedText:strDes];
        [_phoneLabel sizeToFit];
    }
    return _phoneLabel;
}

-(UITextField *)phoneTextF{
    if(_phoneTextF == NULL){
        _phoneTextF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 100, 17)];
        _phoneTextF.left = self.phoneLabel.right + 20;
        _phoneTextF.top = self.receiverLineView.bottom + 19;
        _phoneTextF.font = [UIFont systemFontOfSize:15];
        _phoneTextF.textColor = kBlack999;
        _phoneTextF.keyboardType = UIKeyboardTypeDefault;
        //        _houseNumberTextF.delegate = self;
        _phoneTextF.placeholder = @"联系电话";
    }
    
    return _phoneTextF;
}


-(UIView *)phoneLineView{
    if(_phoneLineView == NULL){
        _phoneLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _phoneLineView.left = 20;
        _phoneLineView.top = self.phoneLabel.bottom + 19;
        _phoneLineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _phoneLineView;
}


-(UILabel *)markTipsLabel{
    if(_markTipsLabel == NULL){
        _markTipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _markTipsLabel.left = 20;
        _markTipsLabel.top = self.phoneLineView.bottom + 19;
        _markTipsLabel.font = [UIFont systemFontOfSize:15];
        _markTipsLabel.textColor = kBlack333;
        NSMutableAttributedString * strDes = [[NSMutableAttributedString alloc]initWithString:@"退货原因*"];
        NSRange range = NSMakeRange(4,1);
        [strDes addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [_markTipsLabel setAttributedText:strDes];
        [_markTipsLabel sizeToFit];
    }
    return _markTipsLabel;
}


-(UILabel *)markLabel{
    if(_markLabel == NULL){
        _markLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - self.markMoreIV.width - self.markTipsLabel.width - 40, 17)];
        _markLabel.left = self.markTipsLabel.right + 20;
        _markLabel.top = self.phoneLineView.bottom + 19;
        _markLabel.font = [UIFont systemFontOfSize:15];
        _markLabel.textColor = kBlack999;
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showReasonPicker)];
        [_markLabel addGestureRecognizer:tap];
        _markLabel.userInteractionEnabled = YES;
        
    }
    return _markLabel;
}

-(UIImageView*)markMoreIV{
    if(_markMoreIV == NULL){
        _markMoreIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
        _markMoreIV.left = kScreenWidth -  _markMoreIV.width -20 ;
        _markMoreIV.top = self.phoneLineView.bottom + 16;
        _markMoreIV.image = IMG(@"more_icon_me_default");
        

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showReasonPicker)];
        [_markMoreIV addGestureRecognizer:tap];
        _markMoreIV.userInteractionEnabled = YES;
        
    }
    return _markMoreIV;
}

-(UIView *)markLineView{
    if(_markLineView == NULL){
        _markLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _markLineView.left = 20;
        _markLineView.top = self.markMoreIV.bottom + 16;
        _markLineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _markLineView;
}


-(UILabel *)desLabel{
    if(_desLabel == NULL){
        _desLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _desLabel.left = 20;
        _desLabel.top = self.markLineView.bottom + 19;
        _desLabel.font = [UIFont systemFontOfSize:15];
        _desLabel.textColor = kBlack333;
        
        NSMutableAttributedString * strDes = [[NSMutableAttributedString alloc]initWithString:@"描述（选填）"];
        NSRange range = NSMakeRange(2,4);
        [strDes addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10]  range:range];
        [strDes addAttribute:NSForegroundColorAttributeName value:kBlack999 range:range];
        [_desLabel setAttributedText:strDes];
        [_desLabel sizeToFit];
        [_desLabel sizeToFit];
    }
    return _desLabel;
}

-(UITextField *)desTextF{
    if(_desTextF == NULL){
        _desTextF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 100, 17)];
        _desTextF.left = self.phoneLabel.right + 21;
        _desTextF.top = self.markLineView.bottom + 19;
        _desTextF.font = [UIFont systemFontOfSize:15];
        _desTextF.textColor = kBlack999;
        _desTextF.keyboardType = UIKeyboardTypeDefault;
        _desTextF.placeholder = @"请输入具体原因";
    }
    
    return _desTextF;
}


-(UIView *)desLineView{
    if(_desLineView == NULL){
        _desLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _desLineView.left = 20;
        _desLineView.top = self.desLabel.bottom + 19;
        _desLineView.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _desLineView;
}




-(UIButton *)agreeBtn{
    if(_agreeBtn == NULL){
        _agreeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        _agreeBtn.left = 18;
        _agreeBtn.top = self.desLineView.bottom + 19;
        [_agreeBtn setImage:IMG(@"cartlist_Unselected_icon") forState:UIControlStateNormal];
        [_agreeBtn setImage:IMG(@"cartlist_selected_icon") forState:UIControlStateSelected];
        
        
        [_agreeBtn addTarget:self action:@selector(onClickStaffBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _agreeBtn;
}

-(UILabel *)introductionLabel{
    if(_introductionLabel == NULL){
        _introductionLabel = [[UILabel alloc ]initWithFrame:CGRectMake(0, 0, 75, 17)];
        _introductionLabel.left = self.agreeBtn.right + 7;
        _introductionLabel.top = self.desLineView.bottom + 19;
        _introductionLabel.font = [UIFont systemFontOfSize:15];
        _introductionLabel.textColor = kBlack333;
        
        NSMutableAttributedString * strIntro = [[NSMutableAttributedString alloc]initWithString:@"已阅读退换货条款"];
        NSRange range = NSMakeRange(3,5);
        [strIntro addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(0x69BA45) range:range];
        [_introductionLabel setAttributedText:strIntro];
        [_introductionLabel sizeToFit];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickIntro)];
        [_introductionLabel addGestureRecognizer:tap];
        _introductionLabel.userInteractionEnabled = YES;
        
    }
    return _introductionLabel;
}


-(UIButton *)nextBtn{
    if(_nextBtn == NULL){
        _nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        _nextBtn.top = kScreenHeight - kStatusBarHeight -  50;
        _nextBtn.backgroundColor = RGBCOLOR(0x69BA45);
        _nextBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        _nextBtn.titleLabel.textColor = [UIColor whiteColor];
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextBtn addTarget:self action:@selector(onClickAdd) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}


- (void)showReasonPicker{
    [self.receiverTextF resignFirstResponder];
    [self.phoneTextF resignFirstResponder];
    [self.desTextF resignFirstResponder];
    
    __weak __typeof(&*self)weakSelf = self;
    
    self.addressPicker = [[EFAddressMarkViewPicker alloc] initWithArray:self.reasonArray];
    [self.addressPicker showWithReason:self.reason];
    self.addressPicker.SuccessBlock2 = ^(EFRetrunReasonItem *reason){
        weakSelf.reason = reason;
        [weakSelf.markLabel setText:reason.reason];
    };
}

-(void)onClickIntro{
    EFH5ViewController* h5 = [[EFH5ViewController alloc]initWithUrlString:@"http://gzt.eatown.puziapp.com/protocol/protocol.html"];
    [ICNavigator pushViewController:h5 animated:YES];
}


@end
